<template>
  <el-dialog
    v-if="open"
    class="dia-style"
    :title="title"
    :visible.sync="open"
    width="1000px"
    :close-on-click-modal="false"
    @close="cancel"
  >
    <el-form
      ref="form"
      :model="formData"
      label-width="140px"
      label-position="left"
      style="padding: 0 10px"
      @submit.native.prevent
    >
      <!-- 基础信息部分 -->
      <div class="upload-title-box">
        <div class="upload-title">基础信息</div>
        <el-checkbox v-model="showRequired" @change="changeShow">仅展示必填信息</el-checkbox>
      </div>
      <HkForm
        ref="hkFormRef1"
        :form-labels="basicArr"
        :rules="formRules"
        :data="formData"
        :dict-list="dictList"
        :btn-hidden="true"
        label-width="140"
        label-position="left"
      >
        <template #filter-equipmentOrgId>
          <treeselect
            v-model="formData.equipmentOrgId"
            :options="dictList.orgList"
            :normalizer="normalizer"
            :append-to-body="true"
            style="width: 100%"
            placeholder="请选择责任部门"
            @select="changeOrg"
            noResultsText="无匹配数据" 
          />
        </template>
        <template #filter-equipmentUserId>
          <el-select
            v-model="formData.equipmentUserId"
            filterable
            placeholder="请选择责任人"
            style="width: 100%"
            :clearable="true"
          >
            <el-option v-for="item in userListData" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </template>
      </HkForm>

      <div>
        <el-collapse v-model="activeNames">
          <el-collapse-item title="重要程度" name="1">
            <HkForm
              ref="hkFormRef"
              :form-labels="criticalityArr"
              :rules="formRules"
              :data="formData"
              :dict-list="dictList"
              :btn-hidden="true"
              label-width="140"
              label-position="left"
            />
          </el-collapse-item>
          <el-collapse-item title="定密信息" name="2">
            <HkForm
              ref="hkFormRef"
              :form-labels="secArr"
              :rules="formRules"
              :data="formData"
              :dict-list="dictList"
              :btn-hidden="true"
              label-width="140"
              label-position="left"
            />
          </el-collapse-item>

          <el-collapse-item title="配置信息" name="3">
            <HkForm
              ref="hkFormRef"
              :form-labels="configArr"
              :rules="formRules"
              :data="formData"
              :dict-list="dictList"
              :btn-hidden="true"
              label-width="140"
              label-position="left"
            />
          </el-collapse-item>
          <el-collapse-item title="维保信息" name="4">
            <HkForm
              ref="hkFormRef"
              :form-labels="maintenanceArr"
              :rules="formRules"
              :data="formData"
              :dict-list="dictList"
              :btn-hidden="true"
              label-width="140"
              label-position="left"
            />
          </el-collapse-item>
          <el-collapse-item title="扩展信息" name="5">
            <el-row :gutter="20" class="iRow">
              <el-col :span="24">
                <el-form-item label="备注">
                  <el-input v-model="formData.equipmentRemark" type="textarea" maxlength="250" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-collapse-item>
        </el-collapse>
      </div>
    </el-form>
    <div v-if="subBtn" slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">保 存</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import HkForm from '@/components/Hk-Form'
import { addDatas, editDatas, queryById, getNo, queryUserList } from '@/api/assets/storageMedium.js'
import AddTable from '@/components/Common/addTable.vue'
import Treeselect from '@riophae/vue-treeselect'

export default {
  components: { HkForm, AddTable, Treeselect },
  props: {
    // 标题
    title: {
      type: String,
      default: ''
    },
    dictList: {
      type: Object,
      default: () => {}
    },
    subBtn: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      // 对话框是否显示
      open: false,
      // 仅展示必填信息
      showRequired: false,
      // 折叠菜单展示的数据
      activeNames: ['1', '2', '3', '4', '5'],
      // 表单数据
      formData: {
        equipmentNo: ''
      },
      // 人员信息
      userListData: [],
      // 基础信息
      basicArr: [
        {
          label: '介质名称',
          field: 'name',
          type: 'input',
          maxlength: 50
        },
        {
          label: '介质编号',
          field: 'equipmentNo',
          type: 'input',
          maxlength: 50
        },
        {
          label: '介质类型',
          field: 'equipmentType',
          type: 'select',
          list: 'equipmentMediumTypeList'
        },
        {
          label: '是否涉密专用设备',
          field: 'equipmentSecretRelated',
          type: 'select',
          list: 'equipmentLocalizedList'
        },
        {
          label: '责任部门',
          field: 'equipmentOrgId',
          type: 'slot'
        },
        {
          label: '责任人',
          field: 'equipmentUserId',
          type: 'slot'
        },
        {
          label: '使用状态',
          field: 'equipmentUseStatus',
          type: 'select',
          list: 'equipmentUseStatusList'
        },
        {
          label: '介质状态',
          field: 'equipmentStatus',
          type: 'select',
          list: 'equipmentStatusList'
        },
        {
          label: '涉密等级',
          field: 'equipmentSecLevel',
          type: 'select',
          list: 'equipmentSecLevelList'
        },
        {
          label: '使用范围',
          field: 'equipmentScopeofuse',
          type: 'select',
          list: 'equipmentScopeOfUseList',
          hidden: false
        }
      ],
      // 重要程度
      criticalityArr: [
        {
          label: '重要程度',
          field: 'equipmentCriticalityLevel',
          type: 'select',
          list: 'equipmentCriticalityLevelList',
          span: 12
        },
        {
          label: '是否重点关注',
          field: 'equipmentFocusLevel',
          type: 'select',
          list: 'equipmentLocalizedList'
        }
      ],
      // 定密信息
      secArr: [
        {
          label: '定密时间',
          field: 'equipmentSecDate',
          type: 'datePicker',
          format: 'yyyy-MM-dd'
        },
        {
          label: '有无密级调整',
          field: 'equipmentSeclevelAdjust',
          type: 'select',
          list: 'equipmentSecLevelAdjustList'
        }
      ],
      // 配置信息
      configArr: [
        {
          label: '厂商',
          field: 'equipmentManufacturer',
          type: 'input',
          maxlength: 50
        },
        {
          label: '品牌',
          field: 'equipmentBrand',
          type: 'input',
          maxlength: 50
        },
        {
          label: '型号',
          field: 'equipmentModel',
          type: 'input',
          maxlength: 50
        },
        {
          label: '介质序列号',
          field: 'equipmentSn',
          type: 'input',
          maxlength: 50
        },
        {
          label: 'PID&VID',
          field: 'equipmentPid',
          type: 'input',
          maxlength: 50
        },
        {
          label: '容量描述',
          field: 'equipmentCapacity',
          type: 'input',
          maxlength: 50
        },
        {
          label: '用途说明',
          field: 'equipmentPurpose',
          type: 'select',
          list: 'equipmentPurposeList'
        },
        {
          label: '存放位置',
          field: 'equipmentStorage',
          type: 'input',
          maxlength: 50
        }
      ],
      // 维保信息
      maintenanceArr: [
        {
          label: '介质购置时间',
          field: 'equipmentPurchaseTime',
          type: 'datePicker',
          format: 'yyyy-MM-dd'
        },
        {
          label: '介质启用时间',
          field: 'equipmentEnableTime',
          type: 'datePicker',
          format: 'yyyy-MM-dd'
        },
        {
          label: '过保日期',
          field: 'equipmentOverdueTime',
          type: 'datePicker',
          format: 'yyyy-MM-dd'
        },
        {
          label: '维保厂家名称',
          field: 'equipmentManufacturerName',
          type: 'input',
          maxlength: 50
        },
        {
          label: '维保厂家电话',
          field: 'equipmentManufacturerMobile',
          type: 'input',
          maxlength: 50
        },
        {
          label: '维保记录',
          field: 'equipmentManufacturerRecord',
          type: 'textarea',
          maxlength: 250,
          span: 24
        }
      ],
      // 表单校验
      formRules: {
        name: [{ required: true, message: '不能为空', trigger: 'blur' }],
        equipmentNo: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { validator: this.validateNo, trigger: 'blur' }
        ],
        equipmentType: [{ required: true, message: '不能为空', trigger: 'change' }],
        equipmentSecretRelated: [{ required: true, message: '不能为空', trigger: 'change' }],
        equipmentUserId: [{ required: true, message: '不能为空', trigger: 'change' }],
        equipmentOrgId: [{ required: true, message: '不能为空', trigger: 'change' }],
        equipmentUseStatus: [{ required: true, message: '不能为空', trigger: 'change' }],
        equipmentStatus: [{ required: true, message: '不能为空', trigger: 'change' }],
        equipmentSecLevel: [{ required: true, message: '不能为空', trigger: 'change' }]
      }
    }
  },
  computed: {},
  watch: {},
  activated() {},

  mounted() {},
  methods: {
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    },
    // 是否只展示必填项
    changeShow() {
      if (this.showRequired) {
        this.activeNames = []
      } else {
        this.activeNames = ['1', '2', '3', '4', '5']
      }
      this.basicArr.forEach((item) => {
        if (typeof item.hidden === 'boolean') {
          item.hidden = this.showRequired
        }
      })
    },
    // 清空表单
    resetFields() {
      this.$refs['form'].resetFields()
      this.$refs['hkFormRef1']?.resetForm('formLabels')
      this.$refs['hkFormRef']?.resetForm('formLabels')
    },
    show(id) {
      this.showRequired = true
      this.changeShow()
      return new Promise((resolve, reject) => {
        if (id) {
          queryById(id).then((res) => {
            this.formData = res
            this.getUserList(res.equipmentOrgId)
          })
        } else {
          getNo().then((res) => {
            this.formData.equipmentNo = res
          })
        }
        resolve(this.formData)
      }).then((res) => {
        this.open = true
      })
    },
    // 提交事件
    async submitForm() {
      // let res = await this.$refs.add_table.submitTable();
      const res = await this.$refs.hkFormRef1.handleFormSubmit('formLabels')
      if (this.title == '添加') {
        addDatas(this.formData).then((res) => {
          this.$message.success('新增成功')
          this.cancel()
          this.$emit('handleAdd')
        })
      } else {
        editDatas(this.formData).then((res) => {
          this.$message.success('修改成功')
          this.cancel()
          this.$emit('handleAdd')
        })
      }
    },
    // 关闭按钮
    cancel() {
      this.formData = { equipmentNo: '' }
      this.userListData = []
      this.open = false
    },
    // 部门change事件
    changeOrg(node) {
      if (this.formData.equipmentUserId) {
        this.formData.equipmentUserId = ''
      }
      this.getUserList(node.id)
    },
    /** 查询列表 */
    getUserList(id) {
      queryUserList(id).then((res) => {
        this.userListData = res
      })
    },
    /** 验证编号重复
     * 是否合法No
     * @param value
     * @param callback
     */
    validateNo(rule, value, callback) {
      const that = this
      if (value !== '' && typeof value !== 'undefined' && value != null) {
        getNo(that.formData.id, value).then((res) => {
          if (res == 'false') {
            callback(new Error('该编号已存在'))
          } else {
            callback()
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.upload-title-box {
  height: 60px;
  width: 100%;
  padding: 0 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid rgba($color: #000000, $alpha: 8%);
  .upload-title {
    font-size: 16px;
    font-weight: 700;
    color: #000000;
  }
}
::v-deep {
  .el-collapse-item {
    border-bottom: 1px solid #e6ebf5;
    .el-collapse-item__wrap {
      border-bottom: none;
    }
    .el-collapse-item__content {
      padding-bottom: 0px;
    }
  }
  .iRow {
    margin: 0 !important ;
  }
  .el-dialog__body {
    display: flex;
    flex-direction: column;
  }
  .el-collapse {
    border-top: none;
    .el-collapse-item__header {
      height: 60px;
      font-size: 16px;
      font-weight: 700;
      color: #000000;
      margin-left: 30px;
      border-bottom: none;
    }
    .el-collapse-item__arrow {
      margin: 0 auto 0 10px;
      margin-left: -90px;
    }

    .el-collapse-item__content {
      .el-row {
        width: 100%;
      }
    }
  }
}
</style>
